package com.katsu.myapplib.activity;

import android.app.Activity;

import androidx.appcompat.app.AppCompatActivity;

import java.util.Iterator;
import java.util.Stack;

public class ActivityStack {
    private static Stack<AppCompatActivity> activityStack = null;

    public ActivityStack() {
    }

    public static void init() {
        if(activityStack == null) {

            activityStack = new Stack();
        }

    }

    public static Stack<AppCompatActivity> getActivityStack() {
        return activityStack;
    }

    public static int getActivityStackSize() {
        return activityStack.size();
    }

    public static AppCompatActivity pushActivity(AppCompatActivity activity) {
        return (AppCompatActivity)activityStack.push(activity);
    }

    public static boolean removeActivity(Class<? extends AppCompatActivity> clazz) {
        return activityStack.remove(clazz);
    }

    public static AppCompatActivity getTopActivity() {
        return (AppCompatActivity)activityStack.peek();
    }

    public static boolean popActivity() {
        AppCompatActivity activity = (AppCompatActivity)activityStack.pop();
        activity.finish();
        return false;
    }

    public static void finishActivity(AppCompatActivity activity) {
        activityStack.remove(activity);
        activity.finish();
    }

    public static void finishActivity(Class<? extends AppCompatActivity> clazz) {

//        for (Activity o : activityStack) {
//            if (o.getClass().equals(clazz)) {
//                activityStack.remove(o);
//                o.finish();
//            }
//        }

        Iterator var1 = activityStack.iterator();

        try {
            while(var1.hasNext()) {
                AppCompatActivity o = (AppCompatActivity)var1.next();
                if(o.getClass().equals(clazz)) {
                    var1.remove();
                    o.finish();
                }
            }
        } catch (Exception e){}




    }

    public static void finishActivitys(Class... clazzs) {
        for(int i = 0; i < clazzs.length; ++i) {
            finishActivity(clazzs[i]);
        }

    }

    public static void finishAllActivity() {
        for(int i = 0; i < activityStack.size(); ++i) {
            if(null != activityStack.get(i)) {
                ((Activity)activityStack.remove(i)).finish();
            }
        }

    }

    public static void retainActivity(Class<? extends AppCompatActivity> clazz) {
        Iterator iterator = activityStack.iterator();

        while(iterator.hasNext()) {
            AppCompatActivity activity = (AppCompatActivity)iterator.next();
            if(!activity.getClass().equals(clazz)) {
                finishActivity(activity);
            }
        }

    }

    public static boolean hasActivity(Class<? extends AppCompatActivity> clazz) {
        for(int i = 0; i < activityStack.size(); ++i) {
            if(((AppCompatActivity)activityStack.get(i)).getClass() == clazz) {
                return true;
            }
        }

        return false;
    }

    public static void release() {
        activityStack.clear();
        activityStack = null;
    }
}
